package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.DataFileUtil;
import com.fdb.efp.nls.service.dao.BatCircleaccDao;
import com.fdb.efp.nls.service.domain.BatCircleacc;
import com.fdb.efp.nls.service.facade.BatCircleaccService;
import com.fdb.efp.nls.service.vo.BatCircleaccVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>ServiceImpl: 自助循环贷款中间表				<p>
 * @author chenqm1
 * @date 2018-06-05
 */
@Service("batCircleaccService")
public class BatCircleaccServiceImpl implements BatCircleaccService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BatCircleaccServiceImpl.class);
	
	@Autowired
	private BatCircleaccDao batCircleaccDao;
	
	/**
	 * 新增操作
	 */
	public int insertBatCircleacc(BatCircleaccVO inBatCircleaccVo) {
		logger.debug("当前新增数据为:"+ inBatCircleaccVo.toString());
		int num = 0;
		try {
			BatCircleacc batCircleacc = new BatCircleacc();
			beanCopy(inBatCircleaccVo, batCircleacc);
			num = batCircleaccDao.insertBatCircleacc(batCircleacc);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteAll(BatCircleaccVO inBatCircleaccVo) {
		logger.debug("当前删除数据条件为:"+ inBatCircleaccVo);
		int num = 0;
		try {
			BatCircleacc batCircleacc = new BatCircleacc();
			beanCopy(inBatCircleaccVo, batCircleacc);
			num = batCircleaccDao.deleteAll(batCircleacc);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBatCircleaccVo +"删除的数据条数为"+ num);
		return num;
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	@Override
	public void fileDataInsertTable(File file, BatCircleaccVO clazz, String operation) {
		try {
			List<BatCircleaccVO> readTxt = DataFileUtil.readTxt(file,clazz.getClass(),"batCircleacc.json","GB2312");
			if(readTxt != null && readTxt.size() > 0) {
				int sum = readTxt.size();
				logger.info("文件中总数据量为:"+sum);
				int batchNum = 1000;
				logger.info("分批处理设置每次处理条数:"+batchNum);
				int cycleNum = sum % batchNum == 0 ? sum/batchNum : sum/batchNum +1;
				logger.info("需要执行的处理次数:" + cycleNum);
				for(int i=0;i<cycleNum;i++) {
					logger.info("开始执行第"+(i+1)+"次处理");
					int start = i*batchNum; 
					int end = start + batchNum;
					logger.info("查询数据范围:"+(start+1)+"~"+end);
					List<BatCircleaccVO> subList = new ArrayList<BatCircleaccVO>();
					if(end > readTxt.size()) {
						subList = readTxt.subList(start, readTxt.size());
					}else {
						subList = readTxt.subList(start, end);
					}
					batCircleaccDao.batchInsertBatCircleacc(subList);
				}
				logger.info("文件中数据成功插入成功！");
			}
		} catch (Exception e) {
			logger.error("文件数据插入表过程出错", e);
		}
	}
	

	@Override
	public List<BatCircleaccVO> queryDataByAccount() {
		List<BatCircleacc> batCircleaccs = batCircleaccDao.queryDataByAccount();
		logger.debug("数据信息的结果集数量为:"+ batCircleaccs.size());
		List<BatCircleaccVO> list = null;
		try {
			list = (List<BatCircleaccVO>) beansCopy(batCircleaccs, BatCircleaccVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}
	
}
